package org.example.hello6_messageAck;


import lombok.Data;
import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.RabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

/**
 * rabbitmq配置类
 *
 * @author: jiazhaoxv
 * @date: 2020/9/22 10:57
 */
@Data
@Configuration
@EnableRabbit
//@PropertySource("classpath:rabbitmq.properties")
@Component
public class RabbitMQConfig {


    @Value("${spring.rabbitmq.host}")
    private String host;
    @Value("${spring.rabbitmq.username}")
    private String userName;
    @Value("${spring.rabbitmq.password}")
    private String password;
    @Value("${spring.rabbitmq.port}")
    private Integer port;
    @Value("${spring.rabbitmq.virtual-host}")
    private String virtualHost;

    /**
     * 配置连接
     *
     * @param
     * @return org.springframework.amqp.rabbit.connection.ConnectionFactory
     * @author jiachaoxv
     * @date 2020/9/22 16:16
     */
    @Bean
    public ConnectionFactory connectFactory() {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
        connectionFactory.setAddresses(host);
        connectionFactory.setUsername(userName);
        connectionFactory.setPassword(password);
        connectionFactory.setPort(port);
        connectionFactory.setVirtualHost(virtualHost);
        return connectionFactory;
    }

    /**
     * rabbitmq配置工厂
     *
     * @param
     * @return org.springframework.amqp.rabbit.listener.RabbitListenerContainerFactory<org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer>
     * @author jiachaoxv
     * @date 2020/9/22 16:14
     */
    @Bean(name = "mqFactoryConfig")
    public RabbitListenerContainerFactory<SimpleMessageListenerContainer> factoryConfig() {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        //允许同时消费数量为5
        factory.setConcurrentConsumers(5);
        //允许同时最大消费数量为10
        factory.setMaxConcurrentConsumers(10);
        factory.setReceiveTimeout(10000L);
        //设置手动提交
        factory.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        factory.setConnectionFactory(connectFactory());
        return factory;
    }

    /**
     * 设置rabbitmq模板
     *
     * @param
     * @return org.springframework.amqp.rabbit.core.RabbitTemplate
     * @author jiachaoxv
     * @date 2020/9/22 16:15
     */
    @Bean
    public RabbitTemplate rabbitTemplate() {
        RabbitTemplate template = new RabbitTemplate(connectFactory());
        //template.setDefaultReceiveQueue(queue);//设置默认接收队列
        return template;
    }

}
